﻿#include "opt.h"
#include "define.h"
#include "errorcode.h"
#include <QDir>

cls_opt::cls_opt()
{
    _INIT_LOCK( &m_lock );
}

cls_opt::~cls_opt()
{
    _DEL_LOCK( &m_lock );
}

/**
 *	initialize the opt data
 *
 *	return
 * 		==	0	successful
 * 		!=	0	errorcode
 */
pem_error_t cls_opt::instance()
{
    pem_error_t		res;

    res = _get_exe_path();
    IF_GOTO( 0 != res, EXIT );

    res = _load_option_file();
    IF_GOTO( 0 != res, EXIT );

EXIT:
    return res;
}

/**
 *	release option data
 */
pem_void_t cls_opt::release()
{
    _LOCK( &m_lock );
    memset( &m_opt, 0, sizeof(opt_info) );
    _UNLOCK( &m_lock );
}

/**
 *	copy the opt_info object out
 *
 * 	@opt		[out] the opt_info object
 */
pem_void_t cls_opt::get_opt( opt_info& opt )
{
    _LOCK( &m_lock );
    memcpy( &opt, &m_opt, sizeof(opt_info) );
    _UNLOCK( &m_lock );
}

/**
 *	get log level
 *
 * 	return
 * 		the log level
 */
pem_s32_t cls_opt::get_log_level()
{
    return m_opt.g_level;
}

/**
 * 	get exe path
 *
 * 	return
 * 		==	0	successful
 * 		!=	0	errorcode
 */
pem_error_t cls_opt	::_get_exe_path()
{
    QByteArray		ba	= QDir::currentPath().toLatin1();

    memset( m_opt.g_path, 0, sizeof(m_opt.g_path) );
    strncpy( m_opt.g_path, ba.data(), sizeof(m_opt.g_path) );

    return PEM_OK;
}

/**
 *	load optiong file
 *
 * 	return
 * 		==	0	successful
 * 		!=	0	errorcode
 */
pem_error_t	cls_opt::_load_option_file()
{
    bool				ok;
    QXmlStreamReader 	xml;
    QFile				file("./pem_sys.xml");

    ok = file.open(QIODevice::ReadOnly | QIODevice::Text);
    if ( !ok )
        return  BUILD_ERRORCODE( errno, PEM_OPT_FILEOPEN );

    _LOCK( &m_lock );

    xml.setDevice( &file );
    while( !xml.atEnd() )
    {
        xml.readNext();
        if ( xml.isStartElement() )
        {
            if ( xml.name() == "db" )
                _read_db( xml );
            else if ( xml.name() == "net" )
                _read_net( xml );
            // add other optoions reading
        }
    }

    _UNLOCK( &m_lock );

#ifdef	QT_DEBUG
    m_opt.g_level	= 6;
#endif

    return PEM_OK;
}

/**
 *	read data base option
 *
 *	@xml		[in] the QxmlStreamReader object
 */
pem_void_t cls_opt::_read_db( QXmlStreamReader& xml )
{
    QByteArray ba;

    while( !xml.atEnd() )
    {
        xml.readNext();
        if ( xml.isStartElement() )
        {
            if ( xml.name() == "ip" )
            {
                ba = xml.readElementText().toLatin1();
                strncpy( m_opt.db_ip, ba.data(), sizeof(m_opt.db_ip) );
            }
            else if ( xml.name() == "port" )
            {
                m_opt.db_port = xml.readElementText().toShort();
            }
            else if ( xml.name() == "user" )
            {
                ba = xml.readElementText().toLatin1();
                strncpy( m_opt.db_user, ba.data(), sizeof(m_opt.db_user) );
            }
            else if ( xml.name() == "passwd" )
            {
                ba = xml.readElementText().toLatin1();
                strncpy( m_opt.db_passwd, ba.data(), sizeof(m_opt.db_passwd) );
            }
            else if ( xml.name() == "dbname" )
            {
                ba = xml.readElementText().toLatin1();
                strncpy( m_opt.db_name, ba.data(), sizeof(m_opt.db_name) );
            }
            else if ( xml.name() == "dbset" )
            {
                ba = xml.readElementText().toLatin1();
                strncpy( m_opt.db_set, ba.data(), sizeof(m_opt.db_set) );
            }
        }
        else if ( xml.isEndElement() )
        {
            if ( xml.name() == "db" )
                break;
        }
    }
}

/**
 *	read data base option
 *
 *	@xml		[in] the QxmlStreamReader object
 */
pem_void_t cls_opt::_read_net( QXmlStreamReader& xml )
{
    QByteArray ba;

    while( !xml.atEnd() )
    {
        xml.readNext();
        if ( xml.isStartElement() )
        {
            if ( xml.name() == "shlib" )
            {
                ba = xml.readElementText().toLatin1();
                strncpy( m_opt.net_shlib, ba.data(), sizeof(m_opt.net_shlib) );
            }
        }
        else if ( xml.isEndElement() )
        {
            if ( xml.name() == "net" )
                break;
        }
    }
}
